package com.liang.paul.officelite.fragments;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.liang.paul.officelite.R;
import com.liang.paul.officelite.activities.AddTaskActivity;
import com.liang.paul.officelite.activities.AddTeamActivity;
import com.liang.paul.officelite.adapters.TaskAdapter;
import com.liang.paul.officelite.beams.TaskBeam;
import com.shamanland.fab.FloatingActionButton;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobObject;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;

public class TaskFragment extends Fragment {

    View rootview;
    FloatingActionButton btn_add;
    String teamId;
    ListView tasklist;
    TaskAdapter adapter;
    ArrayList<TaskBeam> datas;

    public TaskFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootview = inflater.inflate(R.layout.fragment_task, container, false);
        teamId = getArguments().getString("teamId");
        initView();

        return rootview;
    }

    private void initView(){
        btn_add = (FloatingActionButton) rootview.findViewById(R.id.btn_task_addtask);



        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), AddTaskActivity.class);
                intent.putExtra("teamId", teamId);
                startActivityForResult(intent, 111);
            }
        });

        tasklist = (ListView) rootview.findViewById(R.id.list_task_list);
        datas = new ArrayList<TaskBeam>();
        adapter = new TaskAdapter(datas, getActivity());
        tasklist.setAdapter(adapter);
        getData();

    }

    private void getData(){
        BmobQuery<TaskBeam> query = new BmobQuery<TaskBeam>();
        query.addWhereEqualTo("teamId", teamId);
        query.setLimit(50);
        query.order("-createdAt");
        query.findObjects(getActivity(), new FindListener<TaskBeam>() {
            @Override
            public void onSuccess(List<TaskBeam> list) {
                for(TaskBeam beam : list){
                    datas.add(beam);
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onError(int i, String s) {
                Toast.makeText(getActivity(), s, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == 111 && resultCode == 112){
            datas.clear();
            adapter.notifyDataSetChanged();
            getData();
        }
    }
}
